package pers.litao.bit.order.controller;

import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.litao.bit.commons.entities.order.OrderEntity;
import pers.litao.bit.commons.utils.R;
import pers.litao.bit.commons.utils.SysUtil;
import pers.litao.bit.commons.vo.order.OrderVo;
import pers.litao.bit.order.service.OrderService;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 李涛
 * @date 2020-12-05 13:44
 */
@RestController
@RequestMapping("/order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 新建购买商品订单
     *
     * @param orderEntity 实体
     * @return r
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R saveOrder(@RequestBody OrderEntity orderEntity, HttpServletRequest request){
        return orderService.saveOrder(orderEntity,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 零售商获取订单
     *
     * @param request
     * @return
     */
    @RequestMapping("/list/{current}")
    public R getOrderList(@PathVariable("current") Long current,HttpServletRequest request){
        return orderService.getOrderList(current,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 供应商获取订单
     *
     * @param current
     * @param request
     * @return
     */
    @RequestMapping("/supplierorderlist/{current}")
    public R getSupplierOrder(@PathVariable("current") Long current, @RequestParam()Map<String,Long> map, HttpServletRequest request){
        return orderService.getSupplierOrder(current, map,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 合并配送订单
     *
     * @param orderVos
     * @param request
     * @return
     */
    @RequestMapping(value = "/distribution",method = RequestMethod.POST)
    public R distributionOrder(@RequestBody List<OrderVo> orderVos,HttpServletRequest request){
        return orderService.distributionOrder(orderVos,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 计算价格
     *
     * @param money
     * @param num
     * @return
     */
    @RequestMapping(value = "/calculate",method = RequestMethod.GET)
    public R calculate(@RequestParam("money") String money,@RequestParam("num") Long num){
        BigDecimal multiply = BigDecimal.valueOf(Double.parseDouble(money)).multiply(BigDecimal.valueOf(num));
        return R.ok(multiply);
    }

    /**
     * 查询订单（id列表）
     *
     * @param ids
     * @param request
     * @return
     */
    @RequestMapping(value ="/listbyids",method = RequestMethod.POST)
    public R getListByIds(@RequestBody List<Long> ids,HttpServletRequest request){
        return orderService.getListByIds(ids,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 订单完成
     *
     * @param ids
     * @param request
     * @return
     */
    @RequestMapping(value = "/ordercomplete",method = RequestMethod.POST)
    public R orderComplete(@RequestBody List<Long> ids,HttpServletRequest request){
        return orderService.orderComplete(ids,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 取消订单
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/cancel/{id}")
    public R cancelOrder(@PathVariable("id") Long id ,HttpServletRequest request){
        return orderService.cancelOrder(id,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 设置零售价
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/setretailprice",method = RequestMethod.GET)
    public R setRetailPrice(@RequestParam("id") Long id,@RequestParam("price") BigDecimal price,HttpServletRequest request){
        return orderService.setRetailPrice(id,price,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 设置数量
     *
     * @param id
     * @param num
     * @param request
     * @return
     */
    @RequestMapping(value = "/setnum",method = RequestMethod.GET)
    public R setNum(@RequestParam("id") Long id,@RequestParam("num") Integer num,HttpServletRequest request){
        return orderService.setNum(id,num,SysUtil.getLoginUserInfo(request));
    }

    /**
     *获取供应商工作台三个卡片信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/card",method = RequestMethod.GET)
    public R getCard(HttpServletRequest request){
        return orderService.getCard(SysUtil.getLoginUserInfo(request));
    }

    /**
     * 获取昨日成交和历史成交
     *
     * @param request
     * @return
     */
    @RequestMapping("/getOrderCount")
    public R getOrderCount(HttpServletRequest request){
        return orderService.getOrderCount(SysUtil.getLoginUserInfo(request));
    }

    /**
     * 零售商盈亏统计
     *
     * @param request
     * @return
     */
    @RequestMapping("/profitAndLoss")
    public R  profitAndLoss(HttpServletRequest request){
        return orderService.profitAndLoss(SysUtil.getLoginUserInfo(request));
    }

    /**
     * 零售商销售统计-漏斗图
     *
     * @param request
     * @return
     */
    @RequestMapping("/funnel")
    public R funnel(HttpServletRequest request){
        return orderService.funnel(SysUtil.getLoginUserInfo(request));
    }

    /**
     * 供应商统计一周订单量
     *
     * @return
     */
    @RequestMapping("/getSupplyWeekOrders")
    public R getSupplyWeekOrders(HttpServletRequest request){
        return orderService.getSupplyWeekOrders(SysUtil.getLoginUserInfo(request));
    }
    /**
     * 供应商获取一周销售额
     *
     * @return
     */
    @RequestMapping("/getSupplyWeekMoney")
    public R getSupplyWeekMoney(HttpServletRequest request){
        return orderService.getSupplyWeekMoney(SysUtil.getLoginUserInfo(request));
    }
    /**
     * 供应商首页统计图
     *
     * @return
     */
    @RequestMapping("/getSupplyMain")
    public R getSupplyMain(HttpServletRequest request){
        return orderService.getSupplyMain(SysUtil.getLoginUserInfo(request));
    }
}
